import { createApp, ref } from 'vue'
import App from '@/App.vue'

const app = createApp(App)

app.config.globalProperties.$loading = ref(false)
//房间对象
app.config.globalProperties.$sORj = ref({})

import { createRouter, createWebHistory, useRouter } from 'vue-router'
const routes = [
  { redirect: '/', path: '/home' },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home/index.vue'),
    children: [
      {
        path: '/',
        name: 'content',
        component: () => import('@/views/content/index.vue'),
      },
      {
        path: '/technology',
        name: 'technology',
        component: () => import('@/views/technology/index.vue'),
      },
      {
        path: '/archive/:id',
        name: 'archive',
        component: () => import('@/views/archive/index.vue'),
      },
      {
        path: '/gobang',
        name: 'gobang',
        component: () => import('@/views/gobang/index.vue'),
      },
      {
        path: '/vsCode',
        name: 'vsCode',
        component: () => import('@/views/vsCode/index.vue'),
      },
    ],
  },

]

//动态路由表
// const quanxian: Array<any> = [
//   {
//     path: '/404',
//     name: '404',
//     component: () => import('@/views/404/index.vue'),
//     children: [
//       {
//         path: '/acc',
//         name: 'acc',
//         component: () => import('@/views/acc/index.vue'),
//       },
//     ],
//   },
// ]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

//获取全局变量
// const {
//   appContext: {
//     config: { globalProperties },
//   },
// } = getCurrentInstance()

// console.log(globalProperties.$loading)
router.beforeEach((to, from, next) => {
  app.config.globalProperties.$loading.value = true

  // console.log(globalProperties.$loading)

  // ...
  // 返回 false 以取消导航
  next()
})
router.afterEach((to, from) => {
  app.config.globalProperties.$loading.value = false
})

// router.beforeEach((to: any, from: any, next) => {
//   if (hasRoute(to)) {

//     //这里generateRoute的第二个参数传入你筛选后的动态路由表
//     const result = generateRoute(to, quanxian)
//     if (result.flag) {
//       router.addRoute(result.route)

//       next(to.fullPath)
//     } else {
//       next('/111')
//     }
//   } else {
//     next()
//   }
// })

// // 判断是否存在已添加的路由
// const hasRoute = (to: any): boolean => {
//   let flag = true
//   router.getRoutes().map((item: any) => {

//     // 判断是否存在动态路由
//     if (item.path.indexOf('/:') != -1) {
//       if (to.path.indexOf(item.path.substr(0, item.path.indexOf('/:'))) != -1) {
//         flag = false
//       }
//     } else {
//       if (item.path == to.path) {

//         flag = false
//       }
//     }
//   })

//   return flag
// }
// // 返回当前需要跳转的路由对象
// const generateRoute = (to: any, list: Array<any>) => {
//   let index: number = 0
//   let flag: boolean = false
//   const toArr: string = to.path
//   list.map((item: any, i: number) => {
//     if (item.path !== toArr && item.children) {
//       item.children.forEach((item1: any) => {
//         if (item1.path == toArr) {
//           flag = true
//           index = i
//         }
//       })
//     } else {
//       if (item.path == toArr) {
//         flag = true
//         index = i
//       }
//     }
//   })

//   return { route: list[index], flag }
// }

export { router, app }
